package demo1;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2022/2/25
 * Time: 19:30
 * Description: 讲解几个重要的接口
 */
/*class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    *//**
     * 比较方式
     * @param
     * @return
     *//*
    @Override
    public int compareTo(Student o) {
        *//*if(this.age > o.age) {
            return 1;
        }else if(this.age == o.age) {
            return 0;
        }else {
            return -1;
        }*//*
        //return this.age-o.age;
        //return (int)(this.score-o.score);
        return this.name.compareTo(o.name);
    }
}*/
class Student {
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
//年龄比较器
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}
class ScoreComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score-o2.score);
    }
}

public class TestDemo2 {

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhansan",18,89.9);
        students[1] = new Student("lisi",98,19.9);
        students[2] = new Student("ab",48,99.99);

     /*   AgeComparator ageComparator = new AgeComparator();
        int ret = ageComparator.compare(students[0],students[1]);
        System.out.println(ret);

        ScoreComparator scoreComparator = new ScoreComparator();
        ret = scoreComparator.compare(students[0],students[1]);
        System.out.println(ret);*/
        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        System.out.println("排序前："+Arrays.toString(students));
        Arrays.sort(students,scoreComparator);
        System.out.println("排序后："+Arrays.toString(students));
    }


    /*public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhansan",18,89.9);
        students[1] = new Student("lisi",98,19.9);
        students[2] = new Student("ab",48,99.99);
        if(students[0].compareTo(students[1]) > 0) {
            System.out.println(">");
        }else {
            System.out.println("<=");
        }
    }*/

    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhansan",18,89.9);
        students[1] = new Student("lisi",98,19.9);
        students[2] = new Student("ab",48,99.99);
        System.out.println("排序前："+Arrays.toString(students));
        Arrays.sort(students);
        System.out.println("排序后："+Arrays.toString(students));
    }


    public static void main1(String[] args) {
        int[] array = {10,8,23,7,3};
        Arrays.sort(array);
        System.out.println("排序后："+Arrays.toString(array));
    }
}
